package com.zf.yichat.controller.goods;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.StGoodsDto;
import com.zf.yichat.dto.StGoodsRedeemedShipmentDto;
import com.zf.yichat.mapper.StGoodsImgMapper;
import com.zf.yichat.mapper.StGoodsMapper;
import com.zf.yichat.mapper.StGoodsRedeemedMapper;
import com.zf.yichat.model.StGoods;
import com.zf.yichat.model.StGoodsImg;
import com.zf.yichat.model.StGoodsRedeemed;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 商品管理
 */
@Controller
@RequestMapping("goods")
@Slf4j
public class StGoodsController extends BaseController {

    @Resource
    private StGoodsMapper goodsMapper;
    @Resource
    private StGoodsImgMapper goodsImgMapper;
    @Resource
    private StGoodsRedeemedMapper goodsRedeemedMapper;

    /**
     * 商品列表
     * @return
     */
    @RequestMapping("/page/list")
    public String index() {
        return "goods/list";
    }

    /**
     * 用户已兑换商品列表
     * @return
     */
    @RequestMapping("/page/redeemedGoodsList")
    public String redeemedGoodsList() {
        return "goods/redeemedGoodsList";
    }

    /**
     * 新增商品
     * @return
     */
    @RequestMapping("/page/add")
    public String add() {
        return "goods/add";
    }

    /**
     * 编辑商品
     * @return
     */
    @RequestMapping("/page/edit")
    public String edit(Integer id) {

        StGoods goods = goodsMapper.selectByPrimaryKey(id);
        getRequest().setAttribute("goods", goods);
        return "goods/edit";
    }


    /**
     * 商品列表
     * @param page
     * @param limit
     * @param title
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public FsResponse goodsList(Integer page, Integer limit, String title, Integer sale, Integer status) {
        Example example = new Example(StGoods.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(title)) {
            criteria.andLike("title", "%" + title + "%");
        }
        if (null != sale) {
            criteria.andEqualTo("sale", sale);
        }
        if (null != status) {
            criteria.andEqualTo("status", status);
        }

        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(goodsMapper.selectByExample(example), data -> {
            StGoods goods = new StGoods();
            BeanUtils.copyProperties(data, goods);
            return goods;
        });
    }

    /**
     * 查询已兑换商品列表
     * @param page
     * @param limit
     * @param goodsTitle
     * @param userId
     * @param shipmentTime
     * @param state
     * @return
     */
    @RequestMapping("/queryRedeemedGoodsList")
    @ResponseBody
    public FsResponse queryRedeemedGoodsList(Integer page, Integer limit,
                                             String goodsTitle,Long userId,String nickName,
                                             String createTime,
                                             String shipmentTime,
                                             String state) {

        PageHelper.startPage(page, limit);
        List<StGoodsRedeemedShipmentDto> list = goodsRedeemedMapper.queryRedeemedGoodsShipmentList(goodsTitle, userId,nickName, state, createTime, shipmentTime);
        return DtoChangeUtils.getPageList(list, data -> {
            StGoodsRedeemedShipmentDto shipmentDto = new StGoodsRedeemedShipmentDto();
            BeanUtils.copyProperties(data, shipmentDto);
            return shipmentDto;
        });
    }

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    public FsResponse addGoods(StGoodsDto goods) {

        // 处理商品图片
        String imageUrl = goods.getGoodsImgages();
        List<String> imgs = JSONArray.parseArray(imageUrl, String.class);


        goods.setCreateTime(new Date());
        goodsMapper.insert(goods);

        Integer goodsId = goods.getId();
        for (String imgUrl : imgs) {
            StGoodsImg goodsImg = new StGoodsImg();
            goodsImg.setGoodsId(goodsId);
            goodsImg.setUrl(imgUrl);
            goodsImg.setCreateTime(new Date());
            goodsImgMapper.insert(goodsImg);
        }

        return FsResponseGen.successMsg("添加成功！");
    }

    /**
     *  编辑商品
     * @param goods
     * @return
     */
    @RequestMapping("/editGoods")
    @ResponseBody
    public FsResponse editGoods(StGoodsDto goods) {

        // 处理商品图片
        String imageUrl = goods.getGoodsImgages();
        List<String> imgs = JSONArray.parseArray(imageUrl, String.class);

        Integer goodsId = goods.getId();

        // 先删除之前的图片
        goodsImgMapper.deleteByGoodsId(goodsId);

        for (String imgUrl : imgs) {
            StGoodsImg goodsImg = new StGoodsImg();
            goodsImg.setGoodsId(goodsId);
            goodsImg.setUrl(imgUrl);
            goodsImg.setCreateTime(new Date());
            goodsImgMapper.insert(goodsImg);
        }



        goodsMapper.updateByPrimaryKeySelective(goods);
        return FsResponseGen.successMsg("编辑成功！");
    }

    /**
     *  删除商品
     * @param goodsId
     * @return
     */
    @RequestMapping("/delGoods")
    @ResponseBody
    public FsResponse delGoods(Integer goodsId) {
        goodsMapper.deleteByPrimaryKey(goodsId);
        return FsResponseGen.successMsg("删除成功！");
    }

    /**
     *  查询商品图片列表
     * @param goodsId
     * @return
     */
    @RequestMapping("/queryGoodsImgs")
    @ResponseBody
    public FsResponse queryGoodsImgs(Integer goodsId) {
        List<StGoodsImg> list = goodsImgMapper.queryGoodsImgs(goodsId);
        return FsResponseGen.successData(list);
    }



    /**
     *  已兑换商品发货功能
     * @param id
     * @return
     */
    @RequestMapping("/redeemedGoodsShipment")
    @ResponseBody
    public FsResponse redeemedGoodsShipment(Integer id, String logistics) {

        StGoodsRedeemed redeemed = new StGoodsRedeemed();
        redeemed.setId(id);
        redeemed.setState(StGoodsRedeemed.STATE_SHIPMENT);
        redeemed.setShipmentTime(new Date());
        redeemed.setLogistics(logistics);
        return FsResponseGen.successData(goodsRedeemedMapper.updateByPrimaryKeySelective(redeemed));
    }



}
